

var express = require('express')
var router = express.Router();
var { Mvs, Prouser, Tiwen, Advise, Goods, Daka, Money } = require('../utils/model');
const { result } = require('underscore');
var { createToken, checkToken } = require('../utils/token')

router.get("/text", (req, res) => {
    res.json({
        code: 200,
        msg: "测试接口成功 successful"
    })
})

router.get("/movie", (req, res) => {
    var limit = req.query.limit * 1 || 0;
    Mvs.find({}, { _id: 0 })
        .limit(limit)
        .sort({ _id: -1 })
        .then(result => {
            res.json({
                code: 200,
                msg: "获取数据成功",
                result
            })
        })
})

router.post("/login", (req, res) => {
    var body = req.body;
    res.json({
        code: 200,
        msg: '登陆成功...',
        body
    })
})




//获取  crsftoken
router.get("/gettoken", (req, res) => {
    // var token = createToken('wh')
    res.json({
        token: "token",
        msg: '获取token成功',
        type: '1',
        code: 200
    })
})

//登录
router.post("/signin", (req, res) => {
    var body = req.body
    Prouser.findOne({
        mobile: body.mobile

    })
        .then(result => {
            if (result) {
                if (result.password == body.password) {
                    if (result.status == 1) {
                        var token = createToken({
                            mobile: body.mobile,
                            password: body.password,
                            roleid: result.roleid,
                            username: result.username,

                        })
                        res.json({
                            code: 200,
                            type: 1,
                            msg: '登录成功',
                            token
                        })
                    } else {
                        res.json({
                            code: 500,
                            type: 0,
                            msg: '账号已停用，无法登录..'
                        })
                    }
                } else {
                    res.json({
                        code: 500,
                        type: 0,
                        msg: '查询失败，用户名和密码不匹配'
                    })
                }
            } else {
                res.json({
                    code: 200,
                    type: 0,
                    msg: '查询失败，用户名和密码不匹配'
                })
            }
        })
})


// 获取个人信息  token
router.post("/getuserinfo", (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Prouser.findOne({
            mobile: mobile
        }).then(result => {
            res.json({
                code: 200,
                msg: '获取信息成功',
                result: result,
                type: 1
            })
        })
    })
})

//添加用户
router.post("/adduser", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body;
        var addroleid = body.roleid
        if (addroleid > roleid) {
            res.json({
                code: 403,
                msg: '你权限不够，不能添加超管用户..',
                type: 0
            })
        } else {
            //添加用户
            Prouser.findOne({
                mobile: body.mobile
            }).then(data => {
                if (data) {
                    res.json({
                        code: 402,
                        msg: '手机号已经存在，请确认数据..',
                        type: 0
                    })
                } else {
                    body.time = new Date()
                    Prouser.insertMany(body)
                        .then(result => {
                            res.json({
                                code: 200,
                                msg: '用户添加成功..',
                                type: 1,
                                result
                            })
                        })
                }
            })
        }
    })
})

//查询用户
router.post("/getuserlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.find({}, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: '获取用户成功..',
                    type: 1,
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})

//根据_id 删除用户
router.post("/deluser", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: '删除用户成功..',
                    type: 1,
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})


//根据_id 修改用户
router.post("/updateusername", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = { ...req.body, time: new Date() }
        // console.log(req.body);
        Prouser.updateMany({
            _id: req.body._id
        }, {
            $set: body
        })
            .then(result => {
                console.log(result);
                res.json({
                    code: 200,
                    msg: '修改用户成功..',
                    type: 1,
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})

//添加体温记录
router.post("/addtiwen", (req, res) => {
    const {
        tiwen
    } = req.body;

    checkToken(req, res, ({ mobile, roleid }) => {
        Prouser.findOne({
            mobile,
        }).then(userinfo => {
            Tiwen.insertMany({
                time: new Date(),
                mobile,
                username: userinfo.username,
                tiwen,
                hesuan: "0",
                is_yin: "1"
            }).then(data => {
                Tiwen.find({ mobile }, {})
                    .sort({ _id: -1 })
                    .then(result => {
                        res.json({
                            code: 200,
                            msg: "添加成功",
                            result,
                            type: 1
                        })
                    })
                    .catch(err => {
                        res.json({
                            code: 500,
                            msg: "服务器异常",
                            err,
                            type: 0
                        })
                    })
            })
        })
    })

})

//获取我的体温
router.post("/mytiwenlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.find({ mobile }, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "查询成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


//根据_id 删除体温记录
router.post("/deltiwen", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: '删除体温成功..',
                    type: 1,
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})

//修改体温
router.post("/updatetiwen", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = { ...req.body, time: new Date() }
        Tiwen.updateMany({
            _id: req.body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: '修改成功..',
                    type: 1,
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})

//根据_id 修改是否检测
router.post("/updatehesuan", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.updateMany({
            _id: req.body._id
        }, {
            $set: {
                hesuan: req.body.hesuan
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 根据_id 修改 核酸结果 
router.post("/updateisyin", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Tiwen.updateMany({
            _id: req.body._id
        }, {
            $set: {
                is_yin: req.body.is_yin
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})



// 获取首页的 banner
router.post("/gethomebanner", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        res.json({
            msg: "获取数据成功",
            code: 200,
            type: 1,
            result: [
                {
                    imgUrl: "https://www.tjh.com.cn/img/guanggubanner.jpg",

                },
                {
                    imgUrl: "https://www.tjh.com.cn/plmnhytf12f3/1/%E5%B9%BF%E5%91%8A/dqpy.jpg",
                    text: "共战疫情！我们是共产党员！"
                },
                {
                    imgUrl: "https://www.tjh.com.cn/plmnhytf12f3/28/%E9%A6%96%E9%A1%B5%E5%B9%BF%E5%91%8A/2020ysj.jpg",

                },
                {
                    imgUrl: "https://www.tjh.com.cn/plmnhytf12f3/28/%E9%A6%96%E9%A1%B5%E5%B9%BF%E5%91%8A/biaozhang.jpg",
                    text: "战疫先锋，中流砥柱！同济医院获全国抗疫先进集体等六项国家级表彰！"
                }
            ]
        })
    })
})

// router.post("/userlist",(req,res)=>{
//     checkToken(req,res,({mobile,roleid})=>{
//         var obj = {}
//         Prouser.find(obj,{})
//         .sort({_id:-1})
//         .then(result=>{
//             res.json({
//                 code:200,
//                 msg:"查询成功",
//                 result,
//                 type:1
//             })    
//         }).catch(err=>{
//             res.json({
//                 code:500,
//                 msg:"服务器异常",
//                 err,
//                 type:0
//             })
//         })
//     })
// })








// 提交意见
router.post("/addadvise", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        console.log(req.body);
        Prouser.findOne({
            mobile,
        }).then(userinfo => {
            Advise.insertMany({
                time: new Date(),
                mobile,
                username: userinfo.username,
                reply: [],
                ...req.body
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "添加成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
        })
    })
})

// 获取意见列表  
router.post("/getadviselist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {}
        Advise.find(obj, {})
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "意见查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 根据_id 删除意见列表 
router.post("/delAdviseOne", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Advise.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "意见删除成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 获取意见 
router.post("/getadvisebyid", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {
            _id: req.body._id
        }
        Advise.findOne(obj, {})
            .then(result => {
                res.json({
                    code: 200,
                    msg: "查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 修改意见 
router.post("/updateadvisebyid", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {
            _id: req.body._id
        }
        Advise.updateMany(obj, {
            $set: {
                time: new Date(),
                title: req.body.title,
                content: req.body.content,
                category: req.body.category
            }
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 提交意见回复
router.post("/addreply", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var { _id, username, tips } = req.body;
        Advise.findOne({
            _id
        }).then(data => {
            var reply = data.reply || [];
            console.log(reply);
            reply.push({
                author: username,
                mobile,
                roleid,
                tips,
                time: new Date(),
                id: reply.length
            })

            Advise.updateMany({
                _id,
            }, {
                $set: {
                    reply
                }
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "回复成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
        })
    })
})

//商品列表
router.post("/goodlist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {}
        var limit = req.body.limit || 0;
        Goods.find(obj, {})
            .limit(limit)
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "商品查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 商品分类去重
router.post("/goodtypelist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Goods.distinct("type")
            .then(result => {
                res.json({
                    code: 200,
                    msg: "商品分类查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 获取商品详情
router.post("/getgoodone", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Goods.findOne({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "商品查询成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})


// 上传头像  
// multer 
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {   // 头像存放的目录 
        cb(null, './public/images')    // . 文件路径
    },
    filename: function (req, file, cb) {  // 新的文件名 
        cb(null, Date.now() + "WH2004" + file.originalname);  //  originalname 原文件名 
    }
})

var upload = multer({ storage: storage }).any();  // 可以上传任何格式的图片


router.post("/uploadpic", upload, (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {

        if (req.files) {
            var avatar = req.files[0].path;  // 头像的服务器地址 
            Prouser.updateMany({
                mobile
            }, {
                $set: {
                    avatar
                }
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "头像上传成功",
                    type: 1,
                    avatar
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    type: 0
                })
            })

        } else {
            res.json({
                code: 400,
                msg: "头像上传失败",
                type: 0
            })
        }
    })
})

//上班打卡
router.post("/adddaka", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body;
        //添加用户
        Prouser.findOne({
            mobile: body.mobile
        }).then(data => {roleid
            if (data) {
                body.time = new Date()
                Daka.insertMany(body)
                    .then(result => {
                        res.json({
                            code: 200,
                            msg: '打卡成功..',
                            type: 1,
                            result
                        })
                    })
            } else {
                res.json({
                    code: 400,
                    msg: "打卡失败",
                    type: 0
                })
            }
        })

    })
})


//打卡列表
router.post("/dakalist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body;
        Daka.find({
            mobile: body.mobile
        }).then(result => {
            res.json({
                code: 200,
                msg: '获取列表成功..',
                type: 1,
                result
            })
        })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})

//新增薪资
router.post("/addmoney", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var body = req.body;
        Money.insertMany(body)
            .then(result => {
                console.log(result);
                res.json({
                    code: 200,
                    msg: '新增成功..',
                    type: 1,
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })

    })
})


//薪资列表
router.post("/moneylist", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => { 
        Money.find({
            
        }).then(result => {

            res.json({
                code: 200,
                msg: '获取列表成功..',
                type: 1,
                result
            })
        })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: '服务器异常..',
                    err,
                    type: 0
                })
            })
    })
})

// 根据_id 删除薪资
router.post("/delmoney", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Money.remove({
            roleid: req.body.roleid
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result,
                    type: 1
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})

// 修改薪资 
router.post("/updatemoney", (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        var obj = {
            roleid: req.body.roleid

        }
        console.log(req.body);
        Money.updateMany(obj, {
            $set: {     
                money:req.body.money
            }
        })
            .then(result => {
                
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result,
                    type: 1
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err,
                    type: 0
                })
            })
    })
})



// //查询用户
// router.post("/getuserlist1", (req, res) => {
//     checkToken(req, res, ({ mobile, roleid }) => {
//         Prouser.find({}, {})
//             .sort({ _id: -1 })
//             .then(data => {
//                 Money.find({
//                     roleid:data.roleid
//                 }).then(result=>{
//                     res.json({
//                         code: 200,
//                         msg: '获取用户成功..',
//                         type: 1,
//                         result,
//                         data
//                     })
//                 })
//                 .catch(err => {
//                     res.json({
//                         code: 500,
//                         msg: '服务器异常11..',
//                         err,
//                         type: 0
//                     })
//                 })
//             })
//             .catch(err => {
//                 res.json({
//                     code: 500,
//                     msg: '服务器异常22..',
//                     err,
//                     type: 0
//                 })
//             })
//     })
// })
module.exports = router